// pages/detect/detect.js
Page({

      /**
       * 页面的初始数据
       */
      data: {
        image:"",
        image_base64:"",
        detect_data:""
      },
    
      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
    
      },
    
      choose_image: function(){
        var that = this
        wx.chooseMedia({
          count: 9,
          mediaType: ['image','video'],
          sourceType: ['album', 'camera'],
          maxDuration: 30,
          camera: 'back',
          success(res) {
            console.log(res.tempFiles[0].tempFilePath)
            console.log(res.tempFiles[0].size)
            that.setData({
              image:res.tempFiles[0].tempFilePath
            })
            that.urlTobase64(res.tempFiles[0].tempFilePath)
          }
        })
      },
    
    
      urlTobase64(url){
         var that = this
        wx.request({
          url:url,
          responseType: 'arraybuffer', //最关键的参数，设置返回的数据格式为arraybuffer
          success:res=>{
            //把arraybuffer转成base64
                let base64 = wx.arrayBufferToBase64(res.data);        
                //不加上这串字符，在页面无法显示的哦
                base64　= 'data:image/jpeg;base64,' + base64　           
                //打印出base64字符串，可复制到网页校验一下是否是你选择的原图片呢
                console.log(base64)　
                that.setData({
                   image_base64:base64
                 })
              }
        })
     },
    
    
      detect: function(){
        var that = this
        wx.request({
          url: 'https://api-cn.faceplusplus.com/facepp/v3/detect', //仅为示例，并非真实的接口地址
          method:'POST',
          data: {
            api_key: 'S3pPsuiHUq8aTVfx5PaN0i9E9A6qZYG2',
            api_secret: 'Ikh4sAg-OyGE53GUI-PNZ1fxwEyL1BeK',
            // image_url:"https://n.sinaimg.cn/sinakd10110/761/w960h1401/20210222/f38f-kkmphps0684724.jpg",
            image_base64:this.data.image_base64,
            return_attributes:"gender,age,beauty"
           },
           
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success (res) {
            console.log(res.data)
            that.setData({
              detect_data:res.data["faces"][0]["attributes"]
        })
      }
    })
      },
    
    
    
      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function () {
    
      },
    
      /**
       * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
 